﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biz_Dal.Biz;
using System.Collections;
 

public partial class The_hien_TH_THONG_TIN_DON_HANG : System.Web.UI.UserControl
{

    BizKhachHang kh;
    DateTime ngdat;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Khach_hang"] != null)
        {
            kh = (BizKhachHang)Session["Khach_hang"];
            Ho_ten.Text = kh.hoTen;
            Email.Text = kh.email;
            Di_dong.Text = kh.dienThoai;
        }
    }

    protected void Ngay_giao_hang_Load(object sender, EventArgs e)
    {
        Ngay_giao_hang.SelectedDate = DateTime.Today.AddDays(1);
    }
    protected void Ngay_dat_hang_Load(object sender, EventArgs e)
    {
        ngdat = DateTime.Now;
        Ngay_dat_hang.Text = ngdat.ToString();
    }
    protected void Ngay_giao_hang_SelectionChanged(object sender, EventArgs e)
    {
        if(!SoSanhNgay(Ngay_giao_hang.SelectedDate))
            Ngay_giao_hang.SelectedDate = DateTime.Today.AddDays(1);
    }

    private bool SoSanhNgay(DateTime ngayGiao)
    {
        DateTime ngayGiaoToiThieu = DateTime.Today.AddDays(1);
        if (ngayGiao > ngayGiaoToiThieu)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        //lưu đơn đặt hàng
        BizDonDatHang dh = new BizDonDatHang(-1, ngdat, Ngay_giao_hang.SelectedDate, double.Parse(Session["So_tien"].ToString()), (BizKhachHang)Session["Khach_hang"], 1);
        dh.Insert();
        //lưu chi tiết đơn hàng
        ArrayList gio_hang = (ArrayList)Session["Gio_hang"];
        int idDonDH = BizDonDatHang.IdMaxDDH();
        foreach (GIO_HANG a in gio_hang)
        {
            BizCTDonDatHang ct = new BizCTDonDatHang(-1,idDonDH,a.Id,a.So_luong,a.Don_gia);
            ct.Insert();
        }
        //reset giỏ hàng
        Session["So_tien"] = 0.0;
        Session["So_sach"] = 0;
        Session["Gio_hang"] = new ArrayList();
        Response.Redirect("~/Trang_web/Trang_gio_hang.aspx");
    }
}